{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "87d98f54",
   "metadata": {},
   "source": [
    "# Week 6 Optional Extra - Deep Neural Network\n",
    "\n",
    "Just to redeem ourselves from the disappointing result\n",
    "\n",
    "I trained the Deep Neural Network in pricer/deep_neural_network.py and I've uploaded weights here. Download this file to the week6 directory:\n",
    "\n",
    "The file `deep_neural_network.pth` here:\n",
    "\n",
    "https://drive.google.com/drive/folders/1uq5C9edPIZ1973dArZiEO-VE13F7m8MK?usp=drive_link"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16d36f7e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dotenv import load_dotenv\n",
    "import os\n",
    "from huggingface_hub import login\n",
    "from pricer.evaluator import evaluate\n",
    "from pricer.deep_neural_network import DeepNeuralNetworkRunner\n",
    "from pricer.items import Item"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61b42fbf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# environment\n",
    "\n",
    "LITE_MODE = False\n",
    "\n",
    "load_dotenv(override=True)\n",
    "hf_token = os.environ['HF_TOKEN']\n",
    "login(hf_token, add_to_git_credential=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac3f0efc",
   "metadata": {},
   "outputs": [],
   "source": [
    "username = \"ed-donner\"\n",
    "dataset = f\"{username}/items_lite\" if LITE_MODE else f\"{username}/items_full\"\n",
    "\n",
    "train, val, test = Item.from_hub(dataset)\n",
    "\n",
    "print(f\"Loaded {len(train):,} training items, {len(val):,} validation items, {len(test):,} test items\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "abef5881",
   "metadata": {},
   "outputs": [],
   "source": [
    "runner = DeepNeuralNetworkRunner(train, val[:1000])\n",
    "runner.setup()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5efcd601",
   "metadata": {},
   "source": [
    "## If you want to train this yourself\n",
    "\n",
    "Then run this - it takes about 4 hours on my M1 Mac hammering the GPU:\n",
    "\n",
    "```python\n",
    "runner.train(epochs=5)\n",
    "runner.save('deep_neural_network.pth')\n",
    "```\n",
    "\n",
    "## Or just download the file `deep_neural_network.pth` here:\n",
    "\n",
    "https://drive.google.com/drive/folders/1uq5C9edPIZ1973dArZiEO-VE13F7m8MK?usp=drive_link\n",
    "\n",
    "And put it in this week6 directory."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43155e26",
   "metadata": {},
   "outputs": [],
   "source": [
    "runner.load('deep_neural_network.pth')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0543d21",
   "metadata": {},
   "outputs": [],
   "source": [
    "def deep_neural_network(item):\n",
    "    return runner.inference(item)\n",
    "\n",
    "evaluate(deep_neural_network, test)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e14c0512",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f84024f2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
